<?php
class talkModel extends model
{
    public static $instance;
    public $errors = array();

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $url
     * @return string
     */
    public function createRedirectUrl($url){
        return $this->config->talk->redirect . "?url=" . base64_encode($url);
    }

    /**
     * 发送个人通知
     * @param $userId
     * @param $msgType
     * @param $body
     * @return bool
     */
    public function send($userId, $msgType, $body)
    {
        $user = $this->loadModel("user")->getById($userId);
        if(!$user->gtalk){
            return false;
        }

        $data = array(
                "touser"=>$user->gtalk,
                "msgtype" => $msgType,
                "$msgType" => $body
        );
        $this->post($this->config->talk->send, $data);
    }

    /**
     * 发送部门通知
     * @param $party
     * @param $msgType
     * @param $body
     */
    public function sendParty($party, $msgType, $body)
    {
        $data = array(
            "toparty"=>$party,
            "msgtype" => $msgType,
            "$msgType" => $body
        );
        $this->post($this->config->talk->send, $data);
    }

    /**
     * 发送webhook
     * @param $body
     * @param string $webhook
     */
    public function sendWebhook($body, $webhook = ""){
        if(!$webhook){
            $webhook = $this->config->talk->webhook;
        }
        $this->post($webhook, $body);
    }

    /**
     * 模拟post进行url请求
     * @param string $url
     * @param array $post_data
     */
    private function post($url = '', $post_data = array())
    {
        if (empty($url) ) {
            return false;
        }
        $data_string = json_encode($post_data);
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        ));
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;

    }

    /**
     * 模拟get进行url请求
     * @param string $url
     * @param array $post_data
     */
    private function get($url = '', $post_data = array())
    {
        if (empty($url) ) {
            return false;
        }

        $o = "";
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . urlencode($v) . "&";
        }
        $request_data = substr($o, 0, -1);
        $url = $url . (strpos($url, "?") > -1 ? "&" :"?") . $request_data;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return json_decode($data, true);
    }
}